package at.com.vn.chess.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

public class MultiColumn extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	JScrollPane scroll;
	JList<String[]> list;
	String[][] columnData;
	int columnCount;

	public MultiColumn() {
		// create element List array
		addElements();
		this.columnCount = 3;
		// set list for JList
		refreshList();
	}

	public MultiColumn(String[][] columnData, int columnCount) {
		this.columnData = columnData;
		this.columnCount = columnCount;
		// set list for JList
		refreshList();
	}

	public void refreshList() {
		initPanel();
		// set list for JList
		list.setListData(columnData);
		// create Renderer and dislpay
		list.setCellRenderer(new MyCellRenderer(columnCount));
	}

	public void initPanel() {

		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints layoutConstraints = new GridBagConstraints();
		setLayout(layout);

		scroll = new JScrollPane();
		list = new JList<String[]>();
		layoutConstraints.gridx = 0;
		layoutConstraints.gridy = 0;
		layoutConstraints.gridwidth = 1;
		layoutConstraints.gridheight = 1;
		layoutConstraints.fill = GridBagConstraints.BOTH;
		layoutConstraints.insets = new Insets(1, 1, 1, 1);
		layoutConstraints.anchor = GridBagConstraints.CENTER;
		layoutConstraints.weightx = 1.0;
		layoutConstraints.weighty = 1.0;
		scroll = new JScrollPane(list,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		layout.setConstraints(scroll, layoutConstraints);
		add(scroll);

		// // create element List array
		// addElements();
		// // set list for JList
		// list.setListData(columnData);
		// // create Renderer and dislpay
		// list.setCellRenderer(new MyCellRenderer());
	}

	public void addElements() {
		// first number is number of rows, second is number of columns
		columnData = new String[4][3];
		// static setting of String[][]
		columnData[0][0] = "William";
		columnData[0][1] = "A";
		columnData[0][2] = "Wilson";
		// dynamic setting of String[][]
		for (int i = 1; i < 4; i++) {
			columnData[i][0] = "William";
			columnData[i][1] = String.valueOf(i * 13);
			columnData[i][2] = String.valueOf(i * 13 + 1);
		}
	}

	public void add(String userID, String Win, String Lost, String Rank,
			String Gold) {
		String[][] value;
		int maxIndex = 0;
		if (columnData != null && columnData.length > 0) {
			value = new String[columnData.length + 1][columnCount];
			// copy old value
			for (int i = 0; i < columnData.length; i++) {
				for (int j = 0; j < columnCount; j++) {
					value[i][j] = columnData[i][j];
				}
			}
			maxIndex = columnData.length;
		} else {
			value = new String[1][columnCount];
		}

		// add new value
		value[maxIndex][0] = userID;
		value[maxIndex][1] = Win;
		value[maxIndex][2] = Lost;
		value[maxIndex][3] = Rank;
		value[maxIndex][4] = Gold;

		columnData = value;

	}

	public String[][] getColumnData() {
		return columnData;
	}

	static class MyCellRenderer extends JPanel implements ListCellRenderer<String[]> {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		ArrayList<JLabel> item;
		int columnCount;

		MyCellRenderer(int columnCount) {
			item = new ArrayList<JLabel>();
			setLayout(new GridLayout(1, columnCount));
			this.columnCount = columnCount;
			for (int i = 0; i < columnCount; i++) {
				item.add(new JLabel());
				item.get(i).setOpaque(true);
				add(item.get(i));
			}
			item.get(0).setPreferredSize(new Dimension(52, 20));
			item.get(1).setPreferredSize(new Dimension(8, 20));
			item.get(2).setPreferredSize(new Dimension(8, 20));
			item.get(3).setPreferredSize(new Dimension(10, 20));
			item.get(4).setPreferredSize(new Dimension(10, 20));

		}

		public Component getListCellRendererComponent(JList<? extends String[]> list, String[] value,
				int index, boolean isSelected, boolean cellHasFocus) {

			for (int i = 0; i < columnCount; i++) {
//				String iData = ((String[]) value)[i];
				String iData = value[i];
				item.get(i).setText(iData);

				if (isSelected) {
					item.get(i).setBackground(list.getSelectionBackground());
					item.get(i).setForeground(list.getSelectionForeground());
				} else {
					item.get(i).setBackground(list.getBackground());
					item.get(i).setForeground(list.getForeground());
				}
			}
			setEnabled(list.isEnabled());
			setFont(list.getFont());
			return this;
		}
	}

	public static void main(String args[]) {
		JFrame jf;
		// create the frame and JList JPanel
		jf = new JFrame("Multi-Column JList Example");

		jf.getContentPane().add(new MultiColumn());

		// display rules
		jf.setResizable(true);
		jf.setLocation(250, 50);
		jf.setBackground( Color.lightGray);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setSize(650, 400);
		jf.setVisible(true);

	}

	public int getItemCount() {
		return columnData.length;
	}

	public String getItem(int i) {
		// get userID
		return columnData[i][0];
	}

	public void removeAllItem() {
		columnData = null;
	}

}
